// pages/index_detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    plan:'',
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = that.data.id;
    if(options){
      id = options.id;
    }
    var user = wx.getStorageSync('user');
    var token = wx.getStorageSync('token');
    wx.request({
      url: 'http://127.0.0.1:8080/system/userPlan/list?planId='+id+'&userId='+user.userId, // 获取每日计划详情接口
      method: 'GET',
      header: {
        'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
        'Authorization': token
      },
      success(res) {
        if (res.data.code==200) {
          that.setData({
            plan : res.data.rows[0],
            id : id
          })
          // 查询今天是否打卡
          wx.request({
            url: 'http://127.0.0.1:8080/system/in/list?planId='+id+'&userId='+user.userId+"&params%5Btoday%5D="+111,
            method: 'GET',
            header: {
              'content-type': 'application/json', 
              'Authorization': token
            },
            success(res) {
              if (res.data.code==200) {
                if(res.data.total > 0){
                  wx.showToast({
                    title: "今天已打卡",
                    icon: 'none',
                  })
                  that.setData({
                    disabled : true
                  })
                  var pages = getCurrentPages();
                  var beforePage = pages[pages.length - 2];
                  beforePage.onLoad(beforePage.options);
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }
            }
          })
        } else {
          wx.showToast({
            title: "初始化计划失败",
            icon: 'none',
          })
        }
      }
    })
  },
  addStar: function () {
    var that = this;
    var user = wx.getStorageSync('user');
    var token = wx.getStorageSync('token');
    if(that.data.plan.cycle-1 == that.data.plan.day){
      wx.request({
        url: 'http://127.0.0.1:8080/system/in', // 计划打卡
        method: 'put',
        header: {
          'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
          'Authorization': token
        },
        data: {
          planId: that.data.id,
          userId: user.userId,
          isEnd: 1
        },
        success(res) {
          var pages = getCurrentPages();
          var beforePage = pages[pages.length - 2];
          beforePage.onLoad(beforePage.options);
          wx.navigateBack({
            delta: 1,
          })
          if (res.data.code==200) {
            wx.showToast({
              title: "打卡成功",
              icon: 'none',
            })
            that.setData({
              disabled : true
            })
          } else {
            wx.showToast({
              title: "打卡失败",
              icon: 'none',
            })
          }
        }
      })
    }else{
      wx.request({
        url: 'http://127.0.0.1:8080/system/in', // 计划打卡
        method: 'post',
        header: {
          'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
          'Authorization': token
        },
        data: {
          planId: that.data.id,
          userId: user.userId,
        },
        success(res) {
          if (res.data.code==200) {
            wx.showToast({
              title: "打卡成功",
              icon: 'none',
            })
            that.setData({
              disabled : true
            })
          } else {
            wx.showToast({
              title: "打卡失败",
              icon: 'none',
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})